/*
 * @(#)XMPPServer.java   2010.01.15 at 08:51:06 PST
 *
 * Tigase Jabber/XMPP Server
 * Copyright (C) 2004-2012 "Artur Hefczyc" <artur.hefczyc@tigase.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. Look for COPYING file in the top folder.
 * If not, see http://www.gnu.org/licenses/.
 *
 * $Rev: 2996 $
 * Last modified by $Author: wojtek $
 * $Date: 2012-08-21 06:29:57 +0800 (Tue, 21 Aug 2012) $
 */

package tigase.server;

//~--- non-JDK imports --------------------------------------------------------

import tigase.conf.ConfiguratorAbstract;

//~--- classes ----------------------------------------------------------------

/**
 * Describe class XMPPServer here.
 *
 *
 * Created: Wed Nov 23 07:04:18 2005
 *
 * @author <a href="mailto:artur.hefczyc@tigase.org">Artur Hefczyc</a>
 * @version $Rev: 2996 $
 */
public final class XMPPServer {

	public static final String  CONFIGURATOR_PROP_KEY = "tigase-configurator";
	private static final String DEF_CONFIGURATOR      = "tigase.conf.Configurator";

	/** Field description */
	public static final String NAME       = "Tigase";
	private static String      serverName = "message-router";


	/**
	 * Creates a new <code>XMPPServer</code> instance.
	 *
	 */
	private XMPPServer() {}

	public static void main(String[] args) {

		Thread.setDefaultUncaughtExceptionHandler(new ThreadExceptionHandler());

		String initial_config =
			"tigase.level=ALL\n" + "tigase.xml.level=INFO\n"
			+ "handlers=java.util.logging.ConsoleHandler\n"
			+ "java.util.logging.ConsoleHandler.level=ALL\n"
			+ "java.util.logging.ConsoleHandler.formatter=tigase.util.LogFormatter\n"
		;

		ConfiguratorAbstract.loadLogManagerConfig(initial_config);

		try {
			String config_class_name = System.getProperty(CONFIGURATOR_PROP_KEY,
							DEF_CONFIGURATOR);

			ConfiguratorAbstract config = (ConfiguratorAbstract) Class.forName(config_class_name).newInstance();
			config.init(args);

			// config = new ConfiguratorOld(config_file, args);
			config.setName("basic-conf");

			String mr_class_name = config.getMessageRouterClassName();
			MessageRouterIfc router =
				(MessageRouterIfc) Class.forName(mr_class_name).newInstance();

			router.setName(serverName);
			router.setConfig(config);
			router.start();
		} catch (Exception e) {
			System.err.println("");
			System.err.println("  --------------------------------------");
			System.err.println("  ERROR! Terminating the server process.");
			System.err.println("  Problem initializing the server: " + e);
			System.err.println("  Please fix the problem and start the server again.");
			e.printStackTrace();
			System.exit(1);
		}
	}

}    